/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package counter.stosc.com;

import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;

/**
 * @author Stosc
 */
public class CounterMIDlet extends MIDlet implements Controller {

    private Display display = null;
    private Model model ;

    private AddCounterUI addCounterUI;
    private ListCounterUI listCounterUI;
    private EditCounterUI editCounterUI;
    private JobConterUI jobConterUI;

    public CounterMIDlet(){
        super();
        if(display == null){
            display = Display.getDisplay(this);
        }
        model =new Model();
        addCounterUI = new AddCounterUI(this);
        listCounterUI = new ListCounterUI(this);
        editCounterUI = new EditCounterUI(this);
        jobConterUI = new JobConterUI(this);
    }

    public void startApp() {
        Counter[] all = model.getAll();
        if( all != null){
            listCounterUI.load(all);
        }
        handleCommand(Controller.SHOW_LIST_COUNTER, null);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void handleCommand(int command, Object arg) {
        switch(command) {
            case Controller.SHOW_LIST_COUNTER:
                show(listCounterUI);
                break;
            case Controller.SHOW_ADD_COUNTER:
                addCounterUI.clear();
                show(addCounterUI);
                break;
            case Controller.DO_ADD_COUNTER:
                model.addCounter((Counter)arg);
                refresh();
                show("新建修学计数器完成", listCounterUI);
                break;
            case Controller.SHOW_EDITZ_COUNTER:
                editCounterUI.load((Counter)arg);
                show(editCounterUI);
                break;
            case Controller.DO_UPDATE_COUNTER:
                model.updateCounter((Counter)arg);
                refresh();
                show("修学计数器已经更新", listCounterUI);
                break;
            case Controller.SHOW_JOB_COUNTER:
                jobConterUI.load((Counter)arg);
                show(jobConterUI);
                break;
            case Controller.DO_REMOVE_COUNTER:
                model.removeCounter((Counter)arg);
                refresh();
                show("删除计数器成功", listCounterUI);
                break;
            case Controller.APP_EXIT:
                destroyApp(true);
                notifyDestroyed();
                break;
        }
    }

    private void refresh() {
        Counter[] all = model.getAll();
        listCounterUI.load(all);
    }
    //提示信息
    private void show(String msg, Displayable d) {
        Alert alert = new Alert("系统提示", msg, null, AlertType.INFO);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert, d);
    }
    private void show(Displayable d) {
        display.setCurrent(d);
    }

    public Display getDisplay() {
        return display;
    }

    public void playCompleteMinuc() {
        try{
            InputStream is = this.getClass().getResourceAsStream("/qq.mid");
            Player p = Manager.createPlayer(is, "audio/midi");
            p.start();
        }catch( IOException e){
            e.printStackTrace();
        }catch(MediaException e){
            e.printStackTrace();
        }        
    }
}
